﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using Microsoft.Win32;
using System.Management;


namespace Citi.Scheduler.WindowsServices
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
            //AfterInstall += ServiceInstaller_AfterInstall;
        }

        private void scheduler_AfterInstall(object sender, InstallEventArgs e)
        {
            var sc = new ServiceController(scheduler.ServiceName);
            //SetAllowInteractWithDestktop(scheduler.ServiceName);
            sc.Start();
        }

        private void SetAllowInteractWithDestktop(string serviceName)
        {
            var ckey = Registry.LocalMachine.OpenSubKey(string.Format(@"SYSTEM\CurrentControlSet\Services\{0}", serviceName), true);

            if (ckey != null)
            {
                if (ckey.GetValue("Type") != null)
                    ckey.SetValue("Type", 288);//((int)ckey.GetValue("Type") | 256));
            }
        }

        private void executionEngine_AfterInstall(object sender, InstallEventArgs e)
        {
            var sc = new ServiceController(executionEngine.ServiceName);
            //SetAllowInteractWithDestktop(executionEngine.ServiceName);
            sc.Start();
        }

        private void monitor_AfterInstall(object sender, InstallEventArgs e)
        {
            var sc = new ServiceController(monitor.ServiceName);
            //SetAllowInteractWithDestktop(monitor.ServiceName);
            sc.Start();
        }
    }
}
